package si.yun.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import si.yun.common.GuavaCommon;
import si.yun.service.SyNavigation;
import si.yun.utils.GuavaUtils;
import si.yun.utils.JsonData;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: DZQ
 * @description: TODO
 * @date: 2021/7/28 20:11
 * @return
 */
@RestController
@RequestMapping("Navigation")
public class SyNavigationController {

    @Autowired
    private SyNavigation service;

   @Autowired
    private GuavaUtils GuavaUtil;

  /**
   * @author: DZQ
   * @description:  从数据库获取导航栏字段 Guava缓存
   * @date: 2021/7/28 20:12
   * @return JsonData
   */
    @RequestMapping("selNavigation")
    public JsonData selNavigation(){
        try{
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.Navigation), () -> {
                List<Map<String, Object>> getNavigation = service.selNavigation();
                System.out.println(getNavigation);
                return getNavigation;
            });
            if(obj instanceof List){
                List<Map<String,Object>> list = (List<Map<String,Object>>) obj;
                System.out.println(list);
                return list.size()!=0?JsonData.buildSuccess(list):JsonData.buildError("服务器端错误");

            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
/**
 * @author: DZQ
 * @description: 账号推出并销毁session周期
 * @date: 2021/7/30 20:10
 * @return 登陆页面
 */

//退出
@RequestMapping("/loginOut")
@ResponseBody
public boolean loginOut(HttpSession session, HttpServletResponse response) {
    //删除session的指定属性
    session.removeAttribute("user");
    session.invalidate();
    return true;
}

}